﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Classes;
using FlightSystemDBLayer;


namespace FlightSystemControlLayer
{
    public class SearcherCtr
    {
        IList<City> listOfCities;
        IList<Flight> listOfFlights;
        ICity dbCity;
        Searcher searcher;
        IFlight dbFlight;

        public SearcherCtr()
        {
            dbCity = new DBCity();
            listOfCities = dbCity.GetDBCities();

            dbFlight = new DBFlight(listOfCities); // initiated flight db class with preset list of cities
            

            searcher = new Searcher(); 

        }
       
        public IList<City> getCityList() { //city list retrieved from database

            return listOfCities;
        }
        
        public Connection DijkstraCtr(City from, City to, int noOfSeats, DateTime departure)
        {

            foreach (City item in listOfCities)
            {
                if (item.Name.Equals(from.Name))
                {

                    from = item;
                   
                }
                if (item.Name.Equals(to.Name))
                {

                    to = item;
                   

                }


            }
            
            listOfFlights = dbFlight.GetSpecifiedFlights(noOfSeats, departure, to);
            searcher.MakeAGraph(listOfCities, listOfFlights);
            Connection conn = searcher.Dijkstra(from, to);
            
            return conn;
        }

        public Connection DijkstraTimeCtr(City from, City to, int noOfSeats, DateTime departure)
        {

            foreach (City item in listOfCities)
            {
                if (item.Name.Equals(from.Name))
                {

                    from = item;
                   
                }
                if (item.Name.Equals(to.Name))
                {

                    to = item;
                   
                }


            }

            listOfFlights = dbFlight.GetSpecifiedFlights(noOfSeats, departure, to);
            searcher.MakeAGraph(listOfCities, listOfFlights);
            Connection conn = searcher.DijkstraTime(from, to);

            return conn;
        }


        public ListOfConnections MakeASearch(City cityFrom, City cityTo, int noOfSeats, DateTime dateTime) 
        {
            foreach (City item in listOfCities)
            {
                if (item.Name.Equals(cityFrom.Name))
                {

                    cityFrom = item;
                    
                }

                if (item.Name.Equals(cityTo.Name))
                {

                    cityTo = item;
                   
                }

            }

            listOfFlights = dbFlight.GetSpecifiedFlights(noOfSeats, dateTime, cityTo);            
            searcher.MakeAGraph(listOfCities, listOfFlights);
            IGraphImpl list = searcher.ReturnGraph();            
            ListOfConnections conList = list.SearchForConnections(cityFrom, cityTo);
            searcher.fillTheConnections(cityFrom, cityTo, conList);            
            return conList;
        }
    }
}
